package com.xii.controller;

import com.xii.constant.MessageConstant;
import com.xii.constant.RedisConstant;
import com.xii.entity.Result;
import com.xii.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisPool;

@Controller
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("send4Order")
    @ResponseBody
    public Result send4Order(String telephone){
        try {
            //工具类生成验证码
            String code4String = ValidateCodeUtils.generateValidateCode4String(4);
            System.out.println("您的code ："+code4String);
            jedisPool.getResource().set(RedisConstant.ORDER_CODE+"_"+telephone,code4String);
            jedisPool.getResource().expire(RedisConstant.ORDER_CODE+"_"+telephone,60);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

}
